﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DungDungShit
{
    class RankingManager
    {
        RankNode[] ranking = new RankNode[20];

        public RankingManager()
        {
            ranking[0] = new RankNode("AAAAAA", 5000, 30);
        }

        // Real rank is ouput + 1
        private int weakestRank()
        {
            for (int i = 0; i < 20; i++)
            {
                if (ranking[i].Score == 0)
                    return i - 1;
            }
            return 19;
        }
        private bool inRank(int score)
        {
            return score > ranking[weakestRank()].Score;
        }
        private int calcRank(int score)
        {
            for (int i = 0; i < 20; i++)
            {
                if (ranking[i].Score < score)
                    return i;
            }
            return 19;
        }
        private void changeBelowRank(int rank)
        {
            if (rank == 19) 
                return;
            for (int i = 19; i >= rank; i--)
            {
                ranking[i] = new RankNode(ranking[i - 1].Name, ranking[i - 1].Score, ranking[i - 1].RescueCount);
            }
        }

        public void insertNode(string name, int score, int rescueCount)
        {
            if (!inRank(score))
                return;
            else
            {
                int rank = calcRank(score);
                changeBelowRank(rank);
                ranking[rank] = new RankNode(name, score, rescueCount);
            }
        }
        public void LoadRanking()
        {
            //FileStream fs = new FileStream(
        }
        public void SaveRanking()
        {
        }
    }

    class RankNode
    {
        string name;
        int score;
        int rescueCount;

        public string Name { get { return name; } }
        public int Score { get { return score; } }
        public int RescueCount { get { return rescueCount; } }

        public RankNode(string name, int score, int rescueCount)
        {
            this.name = name;
            this.score = score;
            this.rescueCount = rescueCount;
        }
    }
}
